<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="用户姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证件类型" prop="registerCertificateType">
        <el-select
          v-model="queryParams.registerCertificateType"
          placeholder="选择证件类型"
          clearable
        >
          <el-option
            v-for="dict in audience_register_certificate_type"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="角色类型" prop="roleId">
        <el-select
          v-model="queryParams.roleId"
          placeholder="选择角色类型"
          clearable
        >
          <el-option
            v-for="dict in roleList"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="联系方式" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="注册日期" prop="name">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="audienceList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="audienceId" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="角色类型" align="center" prop="roleName" />
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="邮箱" align="center" prop="email" width="200" />
      <el-table-column
        label="证件类型"
        align="center"
        prop="registerCertificateType"
        :formatter="registerCertificateTypeFormat"
      />
      <el-table-column label="证件号码" align="center" prop="certificateCode" />
      <el-table-column
        label="注册时间"
        align="center"
        prop="registerDate"
        width="200"
      ></el-table-column>
      <el-table-column
        label="最后登录时间"
        align="center"
        prop="lastLoginDate"
        width="200"
      ></el-table-column>
      <el-table-column
        label="状态"
        align="center"
        prop="status"
        :formatter="statusFormat"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改观众对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1200px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>个人资料</span>
          </div>

          <el-form-item label="姓名" prop="name">
            <el-input
              readonly="readonly"
              v-model="form.name"
              placeholder="请输入姓名"
            />
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input
              readonly="readonly"
              v-model="form.mobile"
              placeholder="请输入手机号"
            />
          </el-form-item>
          <el-form-item label="性别" prop="sexName">
            <el-input
              readonly="readonly"
              v-model="form.sexName"
              placeholder="请输入性别"
            />
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input
              readonly="readonly"
              v-model="form.email"
              placeholder="请输入邮箱"
            />
          </el-form-item>

          <el-form-item label="年龄" prop="age">
            <el-input
              readonly="readonly"
              v-model="form.age"
              placeholder="请输入年龄"
            />
          </el-form-item>
          <el-form-item label="城市" prop="city">
            <el-input
              readonly="readonly"
              v-model="form.city"
              placeholder="请输入城市"
            />
          </el-form-item>
          <el-form-item label="学历" prop="educationTypeName">
            <el-input
              readonly="readonly"
              v-model="form.educationTypeName"
              placeholder="请输入学历"
            />
          </el-form-item>
          <el-form-item label="行业" prop="industryTypeName">
            <el-input
              readonly="readonly"
              v-model="form.industryTypeName"
              placeholder="请输入行业"
            />
          </el-form-item>
        </el-card>

        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>注册信息</span>
          </div>
          <el-form-item label="用户ID" prop="audienceId">
            <el-input
              readonly="readonly"
              v-model="form.audienceId"
              placeholder="请输入用户ID"
            />
          </el-form-item>
          <el-form-item label="注册证件" prop="registerCertificateTypeName">
            <el-input
              readonly="readonly"
              v-model="form.registerCertificateTypeName"
              placeholder="请输入注册证件"
            />
          </el-form-item>
          <el-form-item label="角色名称" prop="roleName">
            <el-input
              readonly="readonly"
              v-model="form.roleName"
              placeholder="请输入角色名称"
            />
          </el-form-item>
          <el-form-item label="证件号码" prop="certificateCode">
            <el-input
              readonly="readonly"
              v-model="form.certificateCode"
              placeholder="请输入证件号码"
            />
          </el-form-item>

          <el-form-item label="有效期" prop="effectivityDate">
            <el-input
              readonly="readonly"
              v-model="form.effectivityDate"
              placeholder="请输入有效期"
            />
          </el-form-item>

          <el-form-item label="注册来源" prop="registerSourceName">
            <el-input
              readonly="readonly"
              v-model="form.registerSourceName"
              placeholder="请输入注册来源"
            />
          </el-form-item>

          <el-form-item label="注册时间" prop="registerDate">
            <el-input
              readonly="readonly"
              v-model="form.registerDate"
              placeholder="请输入注册时间"
            />
          </el-form-item>
        </el-card>

        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>活动轨迹</span>
          </div>
          <el-form-item label="访问次数" prop="visitTime">
            <el-input
              readonly="readonly"
              v-model="form.visitTime"
              placeholder="请输入访问次数"
            />
          </el-form-item>
          <el-form-item label="志愿者" prop="isVolunteerName">
            <el-input
              readonly="readonly"
              v-model="form.isVolunteerName"
              placeholder="请输入志愿者"
            />
          </el-form-item>
          <el-form-item label="活动参加" prop="activityJoinTime">
            <el-input
              readonly="readonly"
              v-model="form.activityJoinTime"
              placeholder="请输入活动参加"
            />
          </el-form-item>
          <el-form-item label="志愿者活动" prop="volunteerActivityTime">
            <el-input
              readonly="readonly"
              v-model="form.volunteerActivityTime"
              placeholder="请输入志愿者活动"
            />
          </el-form-item>
          <el-form-item label="发言次数" prop="speakTime">
            <el-input
              readonly="readonly"
              v-model="form.speakTime"
              placeholder="请输入发言次数"
            />
          </el-form-item>
        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAudience,
  getAudience,
  delAudience,
  addAudience,
  updateAudience,
} from "@/api/system/audience";
import { getDicts } from "@/api/system/dict/data";
import { getAllRole } from "@/api/system/authRole";

export default {
  name: "Audience",
  data() {
    return {
      audience_status: [],
      audience_register_certificate_type: [],
      audience_sex: [],
      audience_education_type: [],
      roleList: [],
      audience_industry_type: [],
      audience_register_source: [],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 观众表格数据
      audienceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRange: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        mobile: null,
        sex: null,
        email: null,
        age: null,
        provinceId: null,
        provinceName: null,
        cityId: null,
        cityName: null,
        educationType: null,
        industryType: null,
        audienceId: null,
        registerCertificateType: null,
        roleId: null,
        roleName: null,
        certificateCode: null,
        status: null,
        effectivityDate: null,
        registerSource: null,
        registerDate: null,
        lastLoginDate: null,
        isDel: null,
        version: null,
        createUserId: null,
        createUserName: null,
        updateUserId: null,
        updateUserName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  async created() {
    let res1 = await getDicts("audience_status");
    if (res1.code == 200) {
      this.audience_status = res1.data;
    }

    let res2 = await getDicts("audience_register_certificate_type");
    if (res2.code == 200) {
      this.audience_register_certificate_type = res2.data;
    }

    let res3 = await getAllRole();
    if (res3.code == 200) {
      this.roleList = res3.data;
    }

    let res4 = await getDicts("audience_sex");
    if (res4.code == 200) {
      this.audience_sex = res4.data;
    }

    let res5 = await getDicts("audience_education_type");
    if (res5.code == 200) {
      this.audience_education_type = res5.data;
    }

    let res6 = await getDicts("audience_industry_type");
    if (res6.code == 200) {
      this.audience_industry_type = res6.data;
    }

    let res7 = await getDicts("audience_register_source");
    if (res7.code == 200) {
      this.audience_register_source = res7.data;
    }

    this.getList();
  },
  methods: {
    statusFormat(row, column) {
      let str = "";
      this.audience_status.map((item) => {
        if (item.dictValue == row.status) {
          str = item.dictLabel;
        }
      });
      return str;
    },
    registerCertificateTypeFormat(row, column) {
      let str = "";
      this.audience_register_certificate_type.map((item) => {
        if (item.dictValue == row.registerCertificateType) {
          str = item.dictLabel;
        }
      });
      return str;
    },
    /** 查询观众列表 */
    getList() {
      this.loading = true;
      if (this.dateRange) {
        this.queryParams.startDate = this.dateRange[0] + " 00:00:00";
        this.queryParams.endDate = this.dateRange[1] + " 00:00:00";
      } else {
        this.queryParams.startDate = "";
        this.queryParams.endDate = "";
      }
      listAudience(this.queryParams).then((response) => {
        this.audienceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        mobile: null,
        sex: null,
        email: null,
        age: null,
        provinceId: null,
        provinceName: null,
        cityId: null,
        cityName: null,
        educationType: null,
        industryType: null,
        audienceId: null,
        registerCertificateType: null,
        roleId: null,
        roleName: null,
        certificateCode: null,
        status: "0",
        effectivityDate: null,
        registerSource: null,
        registerDate: null,
        lastLoginDate: null,
        isDel: null,
        version: null,
        createUserId: null,
        createUserName: null,
        createTime: null,
        updateUserId: null,
        updateUserName: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加观众";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAudience(id).then((response) => {
        this.form = response.data;

        this.audience_sex.map((item) => {
          if (item.dictValue == this.form.sex) {
            this.form.sexName = item.dictLabel;
          }
        });

        this.audience_education_type.map((item) => {
          if (item.dictValue == this.form.educationType) {
            this.form.educationTypeName = item.dictLabel;
          }
        });

        this.audience_industry_type.map((item) => {
          if (item.dictValue == this.form.industryType) {
            this.form.industryTypeName = item.dictLabel;
          }
        });

        this.audience_register_certificate_type.map((item) => {
          if (item.dictValue == this.form.registerCertificateType) {
            this.form.registerCertificateTypeName = item.dictLabel;
          }
        });

        this.audience_register_source.map((item) => {
          if (item.dictValue == this.form.registerSource) {
            this.form.registerSourceName = item.dictLabel;
          }
        });

        this.form.city = this.form.provinceName + this.form.cityName;

        if (this.form.tblAudienceTrack) {
          this.form.visitTime = this.form.tblAudienceTrack.visitTime + "次";
          if (this.form.tblAudienceTrack.isVolunteer == "0") {
            this.form.isVolunteerName = "否";
          } else if (this.form.tblAudienceTrack.isVolunteer == "1") {
            this.form.isVolunteerName = "是";
          }

          this.form.activityJoinTime =
            this.form.tblAudienceTrack.activityJoinTime + "次";
          this.form.volunteerActivityTime =
            this.form.tblAudienceTrack.volunteerActivityTime + "次";
          this.form.speakTime = this.form.tblAudienceTrack.speakTime + "次";
        } else {
          this.form.visitTime = "";
          this.form.isVolunteerName = "";
          this.form.activityJoinTime = "";
          this.form.volunteerActivityTime = "";
          this.form.speakTime = "";
        }
        this.open = true;
        this.title = "修改观众";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAudience(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAudience(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除观众编号为"' + ids + '"的数据项？')
        .then(function () {
          return delAudience(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/audience/export",
        {
          ...this.queryParams,
        },
        `audience_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
